﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using System.Collections;
using System.IO;
using System.Net;
using mshtml;

namespace OneMangaReader
{
    class Chapter
    {
        #region "Attributes"

        // Chapter Name
        private string _chapter_id;
        
        // Chapter URL
        private string _url;

        // Pages in the chapter
        private Page[] _pages;

        // Download Date
        private DateTime _dl_date;

        // Download location
        private string _dl_loc;


        public string Url
        {
            get { return _url; }
            set { _url = value; }
        }

        #endregion

        #region "Properties"
        
        public string ChapterID
        {
            get { return _chapter_id; }
            set { _chapter_id = value; }
        }

        public DateTime DownloadDate
        {
            get { return _dl_date; }
            set { _dl_date = value; }
        }

        public string DownloadLocation
        {
            get { return _dl_loc; }
            set { _dl_loc = value; }
        }

        public Page[] Pages
        {
            get { return _pages; }
        }

        #endregion

        #region "Methods"

        public Chapter(string chapter_id, string url)
        {
            _chapter_id = chapter_id;
            _url = url;
        }

        public void CreatePages(Manga manga)
        {
            // TODO: page 01 suffix
            // --------------------
            // append page one to url
            // so that can get the page no combo
            // have to handle this in a more proper way later

            Uri page_uri;

            Uri.TryCreate(
                new Uri(
                    new Uri(@manga.Url).GetLeftPart(
                    UriPartial.Authority))
                , @_url + "01",
                out page_uri);

            _pages = extractPages(retrieveHTML(page_uri.AbsoluteUri));


        }

        // Retrieve the HTML from the Manga URL
        private string retrieveHTML(string url)
        {
            // WebClient object
            WebClient client = new WebClient();

            // Retrieve resource as a stream
            Stream data = client.OpenRead(new Uri(url));

            // Retrive the text
            StreamReader reader = new StreamReader(data);
            string htmlContent = reader.ReadToEnd();

            // Cleanup
            data.Close();
            reader.Close();

            return htmlContent;
        }

        // Extract Chapters
        private Page[] extractPages(string htmlContent)
        {
            // Obtain the document interface
            IHTMLDocument2 htmlDocument = (IHTMLDocument2)new mshtml.HTMLDocument();
            htmlDocument.write(htmlContent);

            IHTMLDocument2 selContents = (IHTMLDocument2)new mshtml.HTMLDocument();

            ArrayList pageList = new ArrayList();

            // The following is the part where the page numbers are stated
            // <select name="page" id="id_page_select" class="page-select"> ... </select>
             

            foreach (IHTMLElement el in
                (IHTMLElementCollection)htmlDocument.body.all)
            {

                if (el.tagName.Equals("SELECT") &&
                    el.className != null &&
                    el.className.Equals("page-select"))
                {

                    selContents.write(el.outerHTML);

                    foreach (IHTMLElement cElement in
                        (IHTMLElementCollection)selContents.body.all)
                    {
                        if (cElement.tagName.Equals("OPTION"))
                        {
                            pageList.Add(
                                new Page(((IHTMLOptionElement)cElement).value));
                        }
                    }
                }
            }

            Page[] pgs = new Page[pageList.Count];

            for (int i = 0; i < pageList.Count; i++)
            {
                pgs[i] = (Page)pageList[i];
            }

            return pgs;
        }

        #endregion
    }
}
